package com.yunduo.aspect.log;

import com.yunduo.common.JwtUtils;
import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.RetunData.statuscode.impl.FileCode;
import com.yunduo.common.RetunData.statuscode.impl.ResultCode;
import com.yunduo.common.YunDuoUtils;
import com.yunduo.entity.PFile;
import com.yunduo.entity.User;
import com.yunduo.entity.vo.FileObj;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
 * 文件重要操作日志 切面
 */
@Aspect
@Slf4j
@Component
public class FileLogAspect {
    //上传文件日志
    @AfterReturning(value = "execution(* com.yunduo.controller.PFileController.uploadFile(..))",returning = "result")
    public void upLoadFileLog(Object result) throws Throwable {
        //从请求中获取用户
        User user = JwtUtils.getDataByJwtToken(YunDuoUtils.getRequest());
        //获取返回值
        CommonReturn returnData = (CommonReturn)result;
        //是否成功返回
        if(checkReturn(returnData)){
            //成功响应
            PFile file = (PFile)(returnData.getData().get("file"));
            log.info(user.getName()+" "+ file.getId()+":"+ file.getUrl()+ " " +FileCode.UPLOAD_CREATE.getInfo());
        }else{
            //失败响应
            //日志打印
            log.info(user.getName()+FileCode.UPLOAD_FILE_ERROR.getInfo());
        }
    }

//    @AfterReturning(value = "execution(* com.yunduo.controller.PFileController.deleteFile(..))",returning = "result")
//    public void deleteFilesLog(JoinPoint point,Object result){
//        //删除文件的路径
//        String url = (String)(point.getArgs()[0]);
//        //从请求中获取用户
//        User user = JwtUtils.getDataByJwtToken(YunDuoUtils.getRequest());
//        //获取返回值
//        CommonReturn returnData = (CommonReturn)result;
//        //是否成功返回
//        if(checkReturn(returnData)){
//            log.info(user.getName() + " " + url + " " + FileCode.DELETE_CREATE.getInfo());
//        }else{
//            //失败响应
//            //日志打印
//            log.info(user.getName()+ url + FileCode.DELETE_FILE_ERROR.getInfo());
//        }
//    }

    //删除文件日志
    @AfterReturning(value = "execution(* com.yunduo.service.PFileService.remove*(..))",returning = "result")
    public void deleteFilesDetailLog(JoinPoint point,Object result){
        Object param = point.getArgs()[0];
        //删除文件的路径
        String aimFile="删除异常";
        //如果是传入url
        if(param.getClass().getName().equals(String.class.getName())){
            aimFile = (String)param;
        }
        //传入的object
        if(FileObj.class.getName().equals(param.getClass().getName())){
            aimFile = ((FileObj)param).getName();
        }
        //从请求中获取用户
        User user;
        try {
            user = JwtUtils.getDataByJwtToken(YunDuoUtils.getRequest());
        } catch (Exception e){
            user = new User();
            user.setName("删除中...");
        }
        //是否成功返回
        if((boolean)result){
            log.info(user.getName() + " " + aimFile + " " + FileCode.DELETE_CREATE.getInfo());
        }else{
            //失败响应
            //日志打印
            log.info(user.getName()+ aimFile + FileCode.DELETE_FILE_ERROR.getInfo());
        }
    }

    private boolean checkReturn(CommonReturn result) {
        //是否成功返回
        return ResultCode.SUCCESS.getCode().equals(result.getCode());
    }
}
